#!/bin/bash

source ~/.bashrc

echo '==> check args count'
if test $# -ne 3; then
    echo -e "\nUsage: $0 < username > < password > < harbor ip >\n"
    exit 1
fi

which expect || yum install expect -y

username=$1
password=$2
HARBOR_IP=$3

docker_login()
{
    [ -f "/root/.docker/config.json" ] && rm -f /root/.docker/config.json

    # login 
    expect -c "
    set timeout 100
    spawn docker login http://$HARBOR_IP
    expect {
    \"Username:\"   { send \"$username\n\"; exp_continue; }
    \"Password:\" { send \"$password\n\"; }
    \"already exist on the remote system\" { exit 1; }
    }
    expect eof
    "
}

docker_login
